// 商家侧相关api
import { get, post, put, del } from '../common/ajax';
import { BRD_BRAND_URL, BRD_ADMIN_URL, BRD_DISTRIBUTOR_URL, BRD_SPOKESMAN_URL, BRD_FANS_URL, BRD_WAREHOUSE_URL, BRD_ACTIVITY_URL, BRD_WXPAYAPPLYMENT_URL, BRD_SETTLEMENT_URL, BRD_BANKCARD_URL, BRD_AGGR_URL } from '../constants/url';
import { ACTIVITY_STATUS_VALUE, ACTIVITY_OWNER_VALUE } from '../constants/activity';
import { BRADN_INFO_SCENE } from '../constants/manage';
import { updateLocalMangeTargetInfo } from '../biztools/wex';
import { dealAdminListData, dealDistributorListData, dealDistributorRequestListData, dealSpokesmanListData, dealSpokesmanRequestListData, dealFansListData, dealWarehouseListData, dealWxpayPaymentSaveData } from '../biztools/manage';
import { dealActivityListData, dealActivityDetailData, dealActivitySaveData } from '../biztools/activity';
import { dealAccountLogListData, dealWithdrawLogListData } from '../biztools/record';
import { dealActivityStats, dealStatsConsignmentListData, dealSpokesmanRankListData } from '../biztools/stats';
import { setEmptyObj, deepClone } from '../common/utils';

// 品牌相关

/**
 * 获取可开通的品牌列表
 */
export const getPreBrandList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(BRD_BRAND_URL.preList, params);
    return res.data ? res.data.rows : [];
};

/**
 * 开通品牌
 * @param {String} name 名称
 * @param {String} pic 头像
 * @param {String} intro 简介
 * @param {Number} remotePkOfBrand
 * @param {Number} remotePkOfEmpolyee
 */
export const openBrand = async (params) => {
    const res = await post(BRD_BRAND_URL.open, setEmptyObj(params));
    return res.data || false;
};

/**
 * 获取品牌列表
 */
export const getBrandList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(BRD_BRAND_URL.list, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取品牌信息(分场景)
 * @param {String} scene 场景
 */
export const getBrandInfo = async (params = {}) => {
    const { scene = BRADN_INFO_SCENE.index, brandId } = params;
    const res = await get(BRD_BRAND_URL.brandInfo(brandId), { page: scene });
    return res.data || {};
};

/**
 * 修改品牌信息
 * @param {String} homepageBg 背景图
 * @param {String} name 名称
 * @param {String} intro 简介
 * @param {String} pic 头像
 * @param {String} tel 客服电话
 */
export const updateBrandInfo = async (params) => {
    const res = await put(BRD_BRAND_URL.updateInfo(), params);
    // 更新本地保存的用户信息
    updateLocalMangeTargetInfo(res.data);
    return res.data || false;
};

/**
 * 获取品牌的代言配置
 */
export const getEndorsement = async (brandId) => {
    const res = await get(BRD_BRAND_URL.getEndorsement(brandId));
    return res.data || {};
};

/**
 * 设置品牌的代言配置
 * @param {Object} params 
 */
export const setEndorsement = async (params) => {
    const res = await put(BRD_BRAND_URL.setEndorsement(), setEmptyObj(params));
    return res.data || false;
};

/**
 * 获取品牌的分销招募配置
 */
export const getDistributeEndorsement = async (brandId) => {
    const res = await get(BRD_BRAND_URL.getDistributeEndorsement(brandId));
    return res.data || {};
};

/**
 * 设置品牌的分销招募配置
 * @param {Object} params 
 */
export const setDistributeEndorsement = async (params) => {
    const res = await put(BRD_BRAND_URL.setDistributeEndorsement(), setEmptyObj(params));
    return res.data || false;
};

/**
 * 获取品牌的分销供货配置
 */
 export const getDistributeSupply = async (brandId) => {
    const res = await get(BRD_BRAND_URL.getDistributeSupply(brandId));
    return res.data || {};
};

/**
 * 设置品牌的分销供货配置
 * @param {Object} params 
 */
export const setDistributeSupply = async (params) => {
    const res = await put(BRD_BRAND_URL.setDistributeSupply(), setEmptyObj(params));
    return res.data || false;
};

/**
 * 设置品牌的分销供货商品
 * @param {Object} params 
 */
export const setDistributeProduct = async (params) => {
    const res = await put(BRD_BRAND_URL.setDistributeProduct(), setEmptyObj(params));
    return res.data || false;
};

/**
 * 批量设置品牌的分销供货商品
 * @param {Object} params 
 */
export const setDistributeAllProduct = async (params) => {
    const res = await put(BRD_BRAND_URL.setDistributeAllProduct(), setEmptyObj(params));
    return res.data || false;
};

// 品牌管理员相关

/**
 * 获取品牌管理员列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getAdminList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_ADMIN_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealAdminListData(rows) };
};

/**
 * 添加品牌管理员
 * @param {Number|String} mobile 手机号
 */
export const createAdmin = async (mobile) => {
    const res = await post(BRD_ADMIN_URL.create(), { mobile });
    return res.data || false;
};

/**
 * 移除品牌管理员
 * @param {Number|String} adminId 管理员id
 */
export const removeAdmin = async (adminId) => {
    const res = await del(BRD_ADMIN_URL.remove(adminId));
    return res.data || false;
};

/**
 * 品牌管理员备注
 * @param {Number|String} adminId 管理员id
 * @param {String} aliasName 备注名
 */
export const updateAdminAlias = async (params) => {
    const { adminId, aliasName } = params;
    const res = await put(BRD_ADMIN_URL.alias(adminId), { aliasName });
    return res.data || false;
};

// 品牌分销员相关

/**
 * 获取品牌分销员列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getDistributorList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_DISTRIBUTOR_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealDistributorListData(rows) };
};

/**
 * 品牌品牌分销员备注
 * @param {Number|String} dtbId 分销员id
 * @param {String} aliasName 备注名
 */
export const updateDistributorAlias = async (params) => {
    const { dtbId, aliasName } = params;
    const res = await put(BRD_DISTRIBUTOR_URL.alias(dtbId), { aliasName });
    return res.data || false;
};

/**
 * 获取品牌分销员申请列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getDistributorRequestList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_DISTRIBUTOR_URL.requestList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealDistributorRequestListData(rows) };
};

/**
 * 品牌分销员申请审核
 * @param {String} type 类型 approve|reject
 * @param {Number|String} reqId 申请id
 */
export const auditDistributorRequest = async (type, reqId) => {
    const url = type === 'approve' ? BRD_DISTRIBUTOR_URL.requestApprove(reqId) : BRD_DISTRIBUTOR_URL.requestReject(reqId);
    const res = await post(url);
    return res.data || false;
};

// 品牌代言人相关

/**
 * 获取品牌代言人列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getSpokesmanList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_SPOKESMAN_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealSpokesmanListData(rows) };
};

/**
 * 品牌代言人备注
 * @param {Number|String} spnId 代言人id
 * @param {String} aliasName 备注名
 */
export const updateSpokesmanAlias = async (params) => {
    const { spnId, aliasName } = params;
    const res = await put(BRD_SPOKESMAN_URL.alias(spnId), { aliasName });
    return res.data || false;
};

/**
 * 获取品牌代言人申请列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getSpokesmanRequestList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_SPOKESMAN_URL.requestList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealSpokesmanRequestListData(rows) };
};

/**
 * 品牌代言人申请审核
 * @param {String} type 类型 approve|reject
 * @param {Number|String} reqId 申请id
 */
export const auditSpokesmanRequest = async (type, reqId) => {
    const url = type === 'approve' ? BRD_SPOKESMAN_URL.requestApprove(reqId) : BRD_SPOKESMAN_URL.requestReject(reqId);
    const res = await post(url);
    return res.data || false;
};

// 品牌粉丝相关

/**
 * 获取品牌粉丝列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getFansList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_FANS_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealFansListData(rows) };
};

/**
 * 品牌粉丝备注
 * @param {Number|String} fansId 粉丝id
 * @param {String} aliasName 备注名
 */
export const updateFansAlias = async (params) => {
    const { fansId, aliasName } = params;
    const res = await put(BRD_FANS_URL.alias(fansId), { aliasName });
    return res.data || false;
};

// 品牌仓库相关

/**
 * 获取品牌仓库列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getWarehouseList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_WAREHOUSE_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealWarehouseListData(rows) };
};

/**
 * 新建(编辑)仓库
 */
export const editWarehouse = async (params) => {
    const { brandWarehouseId } = deepClone(params);
    delete params.brandWarehouseId;
    const res = brandWarehouseId ? await put(BRD_WAREHOUSE_URL.update(brandWarehouseId), params) : await post(BRD_WAREHOUSE_URL.create(), params);
    return res.data || false;
};

/**
 * 获取仓库信息
 * @param {String} params brandWarehouseId
 */
export const getWarehouseDetail = async (brandWarehouseId) => {
    const res = await get(BRD_WAREHOUSE_URL.detail(brandWarehouseId));
    return res.data || {};
};

// 活动相关

/**
 * 获取活动管理列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 * @param {Object} ownerInfo
 */
export const getActivityList = async (params, ownerInfo) => {
    const res = await get(BRD_ACTIVITY_URL.list(), params);
    return res.data && res.success ? dealActivityListData(res.data.rows, ownerInfo) : [];
};

// 获取活动复制列表
export const getCopyActivityList = async (params, ownerInfo) => {
    params.isAll = true;
    const res = await get(BRD_ACTIVITY_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealActivityListData(rows, ownerInfo) };
};

/**
 * 获取活动信息
 * @param {String} params
 */
export const getActivityDetail = async (params) => {
    const { brandId, activityId, isCopy = false } = params;
    const res = await get(BRD_ACTIVITY_URL.detail(activityId, brandId));
    return res.data ? dealActivityDetailData(res.data, isCopy) : {};
};

/**
 * 新建(编辑)活动
 */
export const editActivity = async (params) => {
    const { brandId, activityId } = params;
    params = dealActivitySaveData(params);
    const res = activityId ? await put(BRD_ACTIVITY_URL.update(activityId, brandId), params) : await post(BRD_ACTIVITY_URL.create(), params);
    return res.data || false;
};

/**
 * 发布活动
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const publishActivity = async (params) => {
    const { brandId, activityId } = params;
    const res = await put(BRD_ACTIVITY_URL.publish(activityId, brandId));
    return res.data || false;
};

/**
 * 停用、启用活动
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 * @param {String} params processStatus 活动状态
 */
export const statusActivity = async (params) => {
    const { brandId, activityId, processStatus } = params;
    if (processStatus !== ACTIVITY_STATUS_VALUE.processing && processStatus != ACTIVITY_STATUS_VALUE.paused && processStatus !== ACTIVITY_STATUS_VALUE.starting) {
        return;
    }
    const url = processStatus === ACTIVITY_STATUS_VALUE.paused ? BRD_ACTIVITY_URL.resume(activityId, brandId) : BRD_ACTIVITY_URL.pause(activityId, brandId);
    const res = await put(url);
    return res.data || false;
};

/**
 * 获取分销员门店活动排名
 */
export const statsDistributorRank = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_ACTIVITY_URL.statsDistributorRank(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows };
};

/**
 * 活动数据统计=>收入
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsIncomeActivity = async (params) => {
    const { brandId, activityId } = params;
    const res = await get(BRD_ACTIVITY_URL.statsIncome(activityId, brandId));
    return res.data ? dealActivityStats(ACTIVITY_OWNER_VALUE.brand, res.data) : {};
};

/**
 * 活动数据统计=>帮卖
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsConsignmentActivity = async (params) => {
    const { brandId, activityId } = params;
    const res = await get(BRD_ACTIVITY_URL.statsConsignment(activityId, brandId));
    res.data && (res.data.spokesmanActivityConsignmentAggrInfos = dealStatsConsignmentListData(res.data.spokesmanActivityConsignmentAggrInfos));
    return res.data || {};
};

/**
 * 活动数据统计=>代言人排名
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsSpokesmanRankActivity = async (params) => {
    const { brandId, activityId, topRankNumber = 10 } = params;
    const res = await get(BRD_ACTIVITY_URL.statsSpokesmanRank(activityId, brandId), { topRankNumber });
    return res.data ? dealSpokesmanRankListData(res.data) : {};
};

// 微信支付申请相关

/**
 * 获取微信支付申请信息
 */
export const getWxpayApplyDetail = async () => {
    const res = await get(BRD_WXPAYAPPLYMENT_URL.detail());
    return res.data || {};
};

/**
 * 新建(编辑)微信支付申请
 */
export const editWxpayApply = async (params) => {
    let { wxpayApplymentId, wxpayApplyFormData } = params;
    wxpayApplyFormData = dealWxpayPaymentSaveData(wxpayApplyFormData);
    const res = wxpayApplymentId ? await put(BRD_WXPAYAPPLYMENT_URL.update(wxpayApplymentId), { wxpayApplyFormData }) : await post(BRD_WXPAYAPPLYMENT_URL.create(), { wxpayApplyFormData });
    return res.data || false;
};

/**
 * 上传微信支付申请图片
 * @param {String} params image base64图片编码
 * @param {String} params imageType 图片类型
 */
export const uploadWxpayApplyMedia = async (params) => {
    const res = await post(BRD_WXPAYAPPLYMENT_URL.media(), params);
    return res.data || false;
};

// 品牌收益、结算信息相关

/**
 * 获取品牌结算信息
 */
export const getSettlementInfo = async () => {
    const res = await get(BRD_SETTLEMENT_URL.detail());
    return res.data || {};
};

/**
 * 商家申请提现
 * @param {Number} amount 金额
 * @param {String} remark 备注
 */
export const applyWithdraw = async (params) => {
    const res = await post(BRD_SETTLEMENT_URL.withdraw(), params);
    return res.data || false;
};

/**
 * 设置品牌的提现银行卡
 */
export const editBankCard = async (params) => {
    const res = await put(BRD_BANKCARD_URL.update(), { ...params });
    return res.data || false;
};

/**
 * 获取资金明细列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getAccountLogList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_SETTLEMENT_URL.accountLogList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealAccountLogListData(rows) };
};

/**
 * 获取提现记录明细
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getWithdrawLogList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(BRD_SETTLEMENT_URL.withdrawLogList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealWithdrawLogListData(rows) };
};

/**
 * 统计品牌的资金收入与支出
 * @param {Number} begin 开始时间
 * @param {Number} end 结束时间
 */
export const getAggrAccountLogsToInOut = async (params) => {
    const res = await get(BRD_AGGR_URL.accountLogsToInOut(), params);
    return res.data || {};
};
